package cn.com.sd.log.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.com.sd.common.util.MyStringUtils;
import cn.com.sd.common.util.Page;
import cn.com.sd.log.LogLever;
import cn.com.sd.log.dao.IServiceLogDAO;
import cn.com.sd.log.dao.ISystemLogDAO;
import cn.com.sd.log.model.ServiceLog;
import cn.com.sd.log.model.SystemLog;
import cn.com.sd.log.service.IServiceLogService;

@Transactional
@Component(value = "serviceLogService")
public class ServiceLogServiceImpl implements IServiceLogService{

	@Resource(name = "serviceLogDAO")
	private IServiceLogDAO serviceLogDAO;
	
	@Resource(name = "systemLogDAO")
	private ISystemLogDAO systemLogDAO;
	
	@Override
	public List<ServiceLog> list(Page page) {
		return serviceLogDAO.getServiceLogList(page);
	}

	@Override
	public ServiceLog detail(Integer id) {
		ServiceLog serviceLog = serviceLogDAO.getObject(id);
		if (serviceLog == null) {
			return null;
		}
		return serviceLog;
	}
	
	@Override
	public Boolean saveOrUpdate(ServiceLog serviceLog) {
		
//		SystemLog systemLog = new SystemLog(MyStringUtils.getCurrentTime(), serviceLog.getService(), serviceLog.getContent() + "--" + serviceLog.getResult());
		
		boolean flag = false;
		try {
			serviceLogDAO.saveObject(serviceLog);
//			systemLog.setLogLevel(LogLever.info.toString());
			flag = true;
			
		} catch (Exception e) {
//			systemLog.setLogLevel(LogLever.error.toString());
			flag = false;
		}
//		systemLogDAO.saveObject(systemLog);
		return flag;
	}
	
	@Override
	public Boolean del(Integer id) {
		try {
			ServiceLog serviceLog = serviceLogDAO.getObject(id);
			if (serviceLog == null) {
				return true;
			}
			serviceLogDAO.deleteObject(serviceLog);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Boolean delBanch(List<Integer> ids) {
		try {
			List<ServiceLog> roles = new ArrayList<ServiceLog>();
			for (Integer id : ids) {
				if (id == null || id <= 0) {
					continue;
				}
				ServiceLog temp = new ServiceLog();
				temp.setId(id);
				roles.add(temp);
			}
			serviceLogDAO.deleteObjects(roles);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Integer getServiceLogCount() {
		int count = serviceLogDAO.getServiceLogCount();
		if (count <= 0) {
			return 0;
		}
		return count;
	}
}
